/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.server;

import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;

/**
 *
 * @author benbryan
 */
public class KeyValueRef {
    private static KVStore kvstore = null;
    public static KVStore getRef() {
        if (kvstore == null){
            PropertiesManager.getProperties();
            try {
                KVStoreConfig kconfig = new KVStoreConfig(PropertiesManager.getKV_StoreName(), PropertiesManager.getHosts());
                kvstore = KVStoreFactory.getStore(kconfig);
            } catch (Exception ex){
                StringBuilder msg = new StringBuilder("Failed to access nosql database at ");
                String hosts[] = PropertiesManager.getHosts();
                for (int i = 0; i < hosts.length; i++){
                    if (i>0) {
                        msg.append(" or ");
                    }
                    msg.append(hosts[i]);
                }
                
                msg.append(" with storename ").append(PropertiesManager.getKV_StoreName());
                msg.append(".  Check settings in ").append(PropertiesManager.getPropertiesFile().getName());
                Logger.getLogger(KeyValueRef.class.getName()).log(Level.SEVERE, msg.toString());
            } 
        }
        return kvstore;
    }
}
